<?php

namespace app\forms\menu;

use app\forms\Model;
use app\models\Item;
use app\models\Menu;
use app\helpers\MappingHelper;

/**
 * 菜单 表单父类
 */
class BaseForm extends Model
{
    public $parentId;
    public $name;
    public $icon;
    public $route;
    public $type;
    public $target;
    public $remark;
    public $level;
    public $status;
    public $sort;

    public function rules()
    {
        return [
            ['parentId', 'integer'],
            ['parentId', 'default', 'value' => 0],

            ['name', 'trim'],
            ['name', 'string', 'length' => [2, 16]],
            ['name', 'required'],

            ['icon', 'trim'],
            ['icon', 'string', 'max' => 16],
            ['icon', 'default', 'value' => ''],

            ['route', 'trim'],
            ['route', 'string', 'max' => 32],
            ['route', 'required'],
            ['route', 'exist', 'targetClass' => Item::class],

            ['type', 'integer'],
            ['type', 'in', 'range' => array_keys(Menu::getTypeMapping())],
            ['type', 'required'],

            ['target', 'integer'],
            ['target', 'in', 'range' => array_keys(Menu::getTargetMapping())],
            ['target', 'required'],

            ['status', 'integer'],
            ['status', 'in', 'range' => array_keys(MappingHelper::statusList())],
            ['status', 'default', 'value' => MappingHelper::STATUS_ACTIVE],

            ['level', 'integer'],
            ['level', 'default', 'value' => 1],

            ['remark', 'trim'],
            ['remark', 'string', 'max' => 64],
            ['remark', 'default', 'value' => ''],

            ['sort', 'integer'],
            ['sort', 'default', 'value' => 0],
        ];
    }
}